﻿using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using NPOI.SS.Formula.Functions;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using SmartConference.Api.Applocations.IServices;
using SmartConference.Api.DTO;
using SmartConference.Api.Filter;
using SmartConference.Domain;
using SmartConference.ErrorCode;
using SmartConference.Infrastructure.Implements;
using SmartConference.Infrastructure.Interfaces;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;

namespace SmartConference.Api.Controllers
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "用户管理")]
    public class UserController : ControllerBase
    {
        private readonly IUserServices userServices;
        private readonly ExportHelper<UserInfo> exportHelper;

        public UserController(IUserServices userServices,ExportHelper<UserInfo> exportHelper)
        {
            this.userServices = userServices;
            this.exportHelper = exportHelper;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="UserNumber">账号</param>
        /// <param name="UserPassword">密码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<UserLoginDTO>> Login([DefaultValue("admin")] string UserNumber, [DefaultValue("123456")] string UserPassword)
        {
            try
            {
                 return await userServices.Login(UserNumber, UserPassword);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 用户列表
        /// </summary>
        /// <param name="IDNumber">用户id</param>
        /// <param name="MemberLevel">会员等级</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<ApiResult<ApiPaging<UserInfo>>> ShowUser(long? IDNumber, string? MemberLevel, DateTime? CreateTime, DateTime? EndTime, [DefaultValue(1)] int pageIndex, [DefaultValue(10)] int pageSize)
        {
            try
            {
                return await userServices.ShowUser(IDNumber, MemberLevel,CreateTime, EndTime, pageIndex, pageSize);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 用户删除
        /// </summary>
        /// <param name="IDNumber">用户Id</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult> DeleteUser(long IDNumber)
        {
            try
            {
                return await userServices.DeleteUser(IDNumber);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 用户编辑
        /// </summary>
        /// <param name="userUpdateInfoDTO">编辑信息</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> UpdateUser(UserUpdateInfoDTO userUpdateInfoDTO)
        {
            try
            {
                return await userServices.UpdateUser(userUpdateInfoDTO);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 导出用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Export()
        {
            var rowName = "用户信息";
            var titleName = "用户信息";
            return File(exportHelper.Export<UserInfo>(rowName, titleName), System.Net.Mime.MediaTypeNames.Application.Octet, "用户信息.xlsx");
        }


        //[HttpGet]
        //public async Task<IActionResult> Export()
        //{
        //    try
        //    {
        //        var book = new XSSFWorkbook();
        //        //工作簿
        //        var sheet = book.CreateSheet("用户信息");
        //        //标题行
        //        var titlerow = sheet.CreateRow(0);
        //        //标题单元格
        //        var titlecontent = titlerow.CreateCell(0);
        //        //给单元格加标题内容
        //        titlecontent.SetCellValue("王玄");
        //        //创建一个样式
        //        var titlestyle = book.CreateCellStyle();
        //        //居中对齐
        //        titlestyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
        //        //设置字体
        //        var titlefont = book.CreateFont();
        //        //字体大小
        //        titlefont.FontHeight = 20 * 20;
        //        titlefont.FontName = "微软雅黑";
        //        //设置字体样式
        //        titlestyle.SetFont(titlefont);
        //        //设置标题样式
        //        titlecontent.CellStyle = titlestyle;
        //        //合并单元格
        //        sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 1));
        //        //属性名称行
        //        var row = sheet.CreateRow(1);
        //        row.CreateCell(0).SetCellValue("用户编号");
        //        row.CreateCell(1).SetCellValue("用户姓名");
        //        //调用服务接口获取用户信息
        //        var list = await userServices.ExportUser();
        //        int i = 2;
        //        foreach (var item in list)
        //        {
        //            var row1 = sheet.CreateRow(i++);
        //            row1.CreateCell(0).SetCellValue(item.IDNumber);
        //            row1.CreateCell(1).SetCellValue(item.UserNumber);
        //        }
        //        byte[] bytes = null;
        //        using (var stream = new System.IO.MemoryStream())
        //        {
        //            book.Write(stream);
        //            bytes = stream.ToArray();
        //        }
        //        return File(bytes, System.Net.Mime.MediaTypeNames.Application.Octet, "用户信息.xlsx");
        //    }
        //    catch (Exception)
        //    {

        //        throw;
        //    }
        //}
    }
}
